package com.homson.coreservice.servicecommon.response;

import com.homson.coreservice.servicecommon.enumeration.BaseCodeEnum;
import static com.homson.coreservice.servicecommon.enumeration.ResponseCodeEnum.SUCCESS;
import static com.homson.coreservice.servicecommon.enumeration.ResponseCodeEnum.ERROR;

import java.util.List;

/**
 * 分页响应类
 * @param <T> 数据实体类型
 * @author: homson_leo
 */
public class PageResponse<T> extends AbstractResponse {
    /**
     * 数据列表
     */
    private T data;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 当前页码
     */
    private Integer currentPage;
    
    /**
     * 每页大小
     */
    private Integer pageSize;
    
    /**
     * 总页数
     */
    private Integer totalPages;
    
    public PageResponse() {
    }
    
    public PageResponse(BaseCodeEnum code, String message, T data, Long total,
                        Integer currentPage, Integer pageSize) {
        super(code, message);
        this.data = data;
        this.total = total;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        if (total != null && pageSize != null && pageSize > 0) {
            this.totalPages = (int) Math.ceil((double) total / pageSize);
        }
    }
    public PageResponse(BaseCodeEnum code, String message,T data,
                        Integer currentPage, Integer pageSize) {
        super(code, message);
        this.data = data;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    public Long getTotal() {
        return total;
    }
    
    public void setTotal(Long total) {
        this.total = total;
    }
    
    public Integer getCurrentPage() {
        return currentPage;
    }
    
    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }
    
    public Integer getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    
    public Integer getTotalPages() {
        return totalPages;
    }
    
    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }
    
    @Override
    public String toString() {
        return "PageResponse{" +
                "data=" + data +
                ", total=" + total +
                ", currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalPages=" + totalPages +
                '}';
    }
    
    /**
     * 构建成功的分页响应
     * @param data 数据列表
     * @param total 总记录数
     * @param currentPage 当前页码
     * @param pageSize 每页大小
     * @param <T> 数据类型
     * @return PageResponse实例
     */
    public static <T> PageResponse<T> ok(T data, Long total, Integer currentPage, Integer pageSize) {
        return new PageResponse<>(SUCCESS, "success", data, total, currentPage, pageSize);
    }
    
    
    public static <T> PageResponse<T> ok(T data, Integer currentPage, Integer pageSize) {
        return new PageResponse<>(SUCCESS, "success", data, currentPage, pageSize);
    }
    
    /**
     * 构建失败的分页响应
     * @param msg 错误信息
     * @param <T> 数据类型
     * @return PageResponse实例
     */
    public static <T> PageResponse<T> fail(String msg) {
        return new PageResponse<>(ERROR, msg, null, 0L, 1, 10);
    }
}
